package com.devops.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devops.admin.mapper.DOSEnvClusterRelationMapper;
import com.devops.admin.model.DOSEnvClusterRelation;
import com.devops.admin.service.DOSEnvClusterRelationService;
import com.devops.admin.util.Assert;
import com.devops.shiro.vm.BaseUserVM;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;


/**
 * @ClassName DOSEnvClusterRelationServiceImpl
 * @Description TODO
 * @Author heyabo
 * @Date 2022/6/17 09:24
 * @Version 1.0
 **/
@Service
public class DOSEnvClusterRelationServiceImpl extends ServiceImpl<DOSEnvClusterRelationMapper, DOSEnvClusterRelation> implements DOSEnvClusterRelationService {

    @Override
    public Boolean bindCluster(DOSEnvClusterRelation relation) {
        BaseUserVM user = (BaseUserVM) SecurityUtils.getSubject().getPrincipal();
        Assert.isTrue(user != null,"用户未验证");
        Assert.isTrue(user.getAdminRole(),"用户操作权限不足");
        Assert.isTrue(relation != null,"参数不能为空");
        Assert.isTrue(relation.getEnvType() != null && relation.getEnvType() > 0,"环境id不能为空");
        Assert.isTrue(relation.getClusterId() != null && relation.getClusterId() > 0,"集群id不能为空");
        LambdaQueryWrapper<DOSEnvClusterRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DOSEnvClusterRelation::getClusterId,relation.getClusterId())
                .eq(DOSEnvClusterRelation::getEnvType,relation.getEnvType());
        long count = this.count(queryWrapper);
        Assert.isTrue(count < 1,"不可重复绑定");
        return this.save(relation);
    }

}
